<template>
  <!-- 物流单号弹出框 -->
  <el-dialog title="填写物流单号" v-model="editVisible" width="30%" draggable>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="150px" v-loading="loading">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="物流公司：" prop="shipping_code">
            <el-select v-model.trim="form.shipping_code" placeholder="请选择物流公司" clearable class="">
              <el-option v-for="item in logisticsList" :label="item.name" :value="item.shipping_code" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="物流单号：" prop="invoice_no">
            <el-input v-model.trim="form.invoice_no" placeholder="物流单号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="disabledbtn" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useState } from "@/hooks/useState.ts";
import type { IExpressOften } from "@/types/express";
import type { IDirectPurchaseOrderTrackingNumber } from "@/types/rentMachineManage/directPurchaseOrderInfo";
import { putMobileOrderShippingInfo, putMobileOrderUpdateShippingInfo } from "@/utils/api/rentMobileOrder";
const { disabledbtn, loading, toggleState } = useState();
interface Props {
  logisticsList: Array<IExpressOften>;
  orderStatus: number;
}
const { logisticsList, orderStatus } = defineProps<Props>();

const emit = defineEmits<{
  handleSuccess: [];
}>();
const editVisible = defineModel<boolean>("editVisible", { required: true });
const form = defineModel<IDirectPurchaseOrderTrackingNumber>("form", { required: true });
const formRef = useTemplateRef("formRef");
const rules = reactive({
  shipping_code: [{ required: true, message: "请选择物流公司", trigger: "blur" }],
  invoice_no: [{ required: true, message: "请填写物流单号", trigger: "blur" }]
});

watch(
  () => editVisible.value,
  newValue => {
    if (newValue) {
      /* 初始化数据 */
    } else {
      formRef.value?.resetFields();
      formRef.value?.clearValidate();
    }
  }
);
/* 数据校验 */
function submitForm() {
  if (!formRef.value) return;
  formRef.value.validate(valid => {
    if (!valid) {
      ElMessage.warning("请填写必填字段");
      return;
    }
    submitFormInfo();
  });
}
/* 保存或编辑信息 */
function submitFormInfo() {
  toggleState(true);
  if (orderStatus == 1) {
    putMobileOrderShippingInfo(form.value)
      .then(res => {
        ElMessage.success(res.msg);
        emit("handleSuccess");
      })
      .catch(() => {})
      .finally(() => {
        toggleState(false);
      });
  } else if (orderStatus == 2) {
    putMobileOrderUpdateShippingInfo(form.value)
      .then(res => {
        ElMessage.success(res.msg);
        emit("handleSuccess");
      })
      .catch(() => {})
      .finally(() => {
        toggleState(false);
      });
  }
}
</script>

<style lang="scss" scoped></style>
